//
//  ZCRegsitVCSendCodeCell.m
//  ZCJZ
//
//  Created by sujeking on 2019/3/26.
//  Copyright © 2019年 jeking. All rights reserved.
//

#import "ZCRegsitVCSendCodeCell.h"
#import "generalToolBox.h"
#import <BlocksKit+UIKit.h>
@interface ZCRegsitVCSendCodeCell()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *inputTF;
@property (weak, nonatomic) IBOutlet UIButton *sendBtn;

@property (nonatomic, strong) NSTimer *timer;
@end


@implementation ZCRegsitVCSendCodeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupSubViews];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(showTimerCount)
                                                 name:@"registsendcode" object:nil];
}

- (void)dealloc {
    [self.timer invalidate];
    self.timer = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"registsendcode" object:nil];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (void)setupSubViews {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.sendBtn.layer.cornerRadius = 3.0f;
    self.sendBtn.layer.masksToBounds = YES;
  self.sendBtn.backgroundColor = [generalToolBox getColorWithHexadecimalColorStr:@"0xF9150F"];
    self.inputTF.delegate = self;
    [self.sendBtn bk_addEventHandler:^(id sender) {
        if (self.btnClickCallback) {
            self.btnClickCallback(self.inputTF.text);
        }
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)showTimerCount {
    self.sendBtn.userInteractionEnabled = NO;
    __block NSInteger ts = 60;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        ts--;
        if (ts == -1) {
            [self.sendBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
            self.sendBtn.userInteractionEnabled = YES;
            [self.timer invalidate];
        } else {
            [self.sendBtn setTitle:[NSString stringWithFormat:@"%zdS",ts] forState:UIControlStateNormal];
        }
    }];
}

// MARK: - UITextFieldDelegate

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (self.endEditClickCallback) {
        self.endEditClickCallback(textField.text);
    }
}
     
@end
